// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Order from "./Order";
import config from "../config/config";
import func from "../config/func";

const { ccclass, property } = cc._decorator;

@ccclass
export default class OrderItem extends cc.Component {

    @property(cc.Sprite)
    m_food: cc.Sprite = null;
    @property(cc.Label)
    m_reward: cc.Label = null;
    @property(cc.Sprite)
    m_state: cc.Sprite = null;
    @property(cc.SpriteFrame)
    m_stateFrame: cc.SpriteFrame[] = [];
    @property(cc.Sprite)
    m_bottem: cc.Sprite[] = [];
    @property(cc.SpriteFrame)
    m_right: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    m_wrong: cc.SpriteFrame = null;
    @property(cc.Sprite)
    m_mask: cc.Sprite = null;

    n_info: OrderInfo = null;
    n_orderNode: Order = null;
    start() {
        this.n_orderNode = cc.find("Canvas/game/orderNode").getComponent(Order);
    }
    init(date: OrderInfo) {
        this.n_info = date;
        this.m_mask.node.active = false;
        this.m_reward.string = this.n_info.reward.toString();
        this.m_state.spriteFrame = this.m_stateFrame[0];
        this.loadPlist();
    }
    /**
     * 加载图集
     */
    async loadPlist() {
        let plist: cc.SpriteAtlas = null;
        if (config.spriteAtlas[config.spriteAtlasPath.food]) {
            plist = config.spriteAtlas[config.spriteAtlasPath.food];
        } else {
            plist = await func.loadResouce(config.spriteAtlasPath.food, cc.SpriteAtlas) as cc.SpriteAtlas;
            config.spriteAtlas[config.spriteAtlasPath.food] = plist;
        }
        for (let i = 0; i < this.m_bottem.length; i++) {
            let id = this.n_info.total[i],
                item = this.m_bottem[i];
            if (id) {
                item.node.active = true;
                let key = `batching_${id}`;
                item.spriteFrame = plist.getSpriteFrame(key);
            } else {
                item.node.active = false;
            }
            item.node.children[0].getComponent(cc.Sprite).spriteFrame = null;
        }
        let key = `vegetableType_${this.n_info.id}`;
        this.m_food.spriteFrame = plist.getSpriteFrame(key);
    }
    /**
     * 蔬菜选择是否正确
     * @param index 
     * @param rightOrWrong 
     */
    changeSelectVegettables(index: number, rightOrWrong: boolean) {
        let child = this.m_bottem[index].node.children[0];
        child.active = true;
        if (rightOrWrong) {
            child.getComponent(cc.Sprite).spriteFrame = this.m_right;
        } else {
            child.getComponent(cc.Sprite).spriteFrame = this.m_wrong;
        }
    }
    /**
     * 修改订单状态
     */
    changeOrderState() {
        this.n_info.complete = config.selectOrder.complete;
        this.m_state.spriteFrame = this.m_stateFrame[1];
        this.m_mask.node.active = true;
    }
    /**
     * 销毁订单
     */
    destroyNode() {
        this.n_orderNode.moveOrderNode(this.n_info, this.node);
    }
}
